//
//  CATransitionController.m
//  CoreAnimationDemo
//
//  Created by AY on 17/5/31.
//  Copyright © 2017年 Alex. All rights reserved.
//

#import "CATransitionController.h"

@interface CATransitionController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;

/**
 *  当前图片的索引
 */
@property (nonatomic, assign) int index;
- (IBAction)previous:(id)sender;
- (IBAction)next:(id)sender;
@end

@implementation CATransitionController

- (instancetype)init {
    self = [super init];
    if (self) {
        self = [self initWithNibName:NSStringFromClass([CATransitionController class]) bundle:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)previous:(id)sender {
    self.index--;
    if (self.index == -1) {
        self.index = 8;
    }

    NSString *filename = [NSString stringWithFormat:@"%d.jpg", self.index + 1];
    self.iconView.image = [UIImage imageNamed:filename];

    CATransition *anim = [CATransition animation];
        anim.type = @"cube";
        anim.subtype = kCATransitionFromLeft;
//    anim.type = @"pageUnCurl";
    anim.duration = 0.5;
    [self.view.layer addAnimation:anim forKey:nil];
}

- (IBAction)next:(id)sender {
    self.index++;
    if (self.index == 9) {
        self.index = 0;
    }

    NSString *filename = [NSString stringWithFormat:@"%d.jpg", self.index + 1];
    self.iconView.image = [UIImage imageNamed:filename];

    // 转场动画
    CATransition *anim = [CATransition animation];
    anim.type = @"pageCurl";
    //    anim.subtype = kCATransitionFromRight;
    anim.duration = 0.5;

    //    anim.startProgress = 0.0;
    //
    //    anim.endProgress = 0.5;

    [self.view.layer addAnimation:anim forKey:nil];
}
@end
